package com.example.superoa.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.model.pojos.Ttest;
import com.example.superoa.model.service.TtestService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Controller
public class TtestController {
		@Autowired
		private TtestService testservice;		
		//查询考试试卷			
		@PostMapping("select-test")
		@ResponseBody
		public Map<String,Object> show(String ttestname,int page,int rows) {
			Map<String,Object> map = new HashMap<>();
			//先分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			JSONArray j = testservice.findBytestname(ttestname);
			int i=1;
			for (Object object : j) {
				JSONObject obj=(JSONObject)object;
				obj.put("test", i);
				
				i++;
			}
			//总记录
			map.put("total", pg.getTotal());
			//页内容
			map.put("rows", j);
			return map;
		}	
		@RequestMapping("conpanytest")
		@ResponseBody
		public JSONArray conpanytest() {
			return testservice.findAllstest();
		}
		//点击查看详情显示
		@RequestMapping("test-xiangq")
		@ResponseBody
		public JSONArray testxiangq(int ttestid) {
			return testservice.findByIdtest(ttestid);
		}
		/**新增
		 * @return
		 */
		@RequestMapping("select-testinserts")
		@ResponseBody
		public String recruitinsert(Ttest test,Integer staff_id) {
			testservice.insertcopany(test,staff_id);
			return "redirect:main-administrator:testconpany";				
		}
		//初始化
		@RequestMapping("test-all")
		@ResponseBody
		public JSONArray alls(int ttestid) {		
			JSONArray json = testservice.selectbyid(ttestid);		
			return json;
		}
		//修改试卷
		@RequestMapping("test-xiugai")
		@ResponseBody
		public void xiugai(int ttestid,String ttestname,String ttesttheme,String ttestledge, Date ttestdate,int ttesttnums) {
			System.err.println("接收到"+ttestid+".."+ttestname+".."+ttesttheme+".."+ttestledge+".."+ttestdate+".."+ttesttnums);
			testservice.xiugai(ttestid,ttestname,ttesttheme,ttestledge,ttestdate,ttesttnums);
			System.err.println(ttestname);
		}	
		
		@PostMapping("sert_dan")
		@ResponseBody
		public Map<String,Object> sertDan(String str,int page,int rows,HttpSession session) {
			Map<String,Object> map = new HashMap<>();
			//先分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			JSONArray j = testservice.findAllDan((List<Integer>) session.getAttribute(str),str);
			int i=1;
			for (Object object : j) {
				JSONObject obj=(JSONObject)object;
				obj.put("test", i);
				i++;
			}
			//总记录
			map.put("total", pg.getTotal());
			//页内容
			map.put("rows", j);
			return map;
		}	
		//通过单选题目id查询题目
		@RequestMapping("chaxun_dan")
		@ResponseBody
		public JSONObject chaxunDan(Integer id,HttpSession session) {
			JSONObject obj=new JSONObject();
			JSONObject tanswer=(JSONObject)testservice.findByIdDai(id);
			JSONObject titem=(JSONObject) testservice.findByIdDan(id);
			List<Integer> list;
			obj.put("titemall", titem.get("titemall"));
			obj.put("staff_name", titem.get("Staff_name"));
			obj.put("titemtypes", titem.get("titemtypes"));
			obj.put("titemid", id);
			if(titem.get("titemtypes").equals("单选")||titem.get("titemtypes").equals("多选")) {
				obj.put("a", tanswer.get("tanswerA"));
				obj.put("b", tanswer.get("tanswerB"));
				obj.put("c", tanswer.get("tanswerC"));
				obj.put("d", tanswer.get("tanswerD"));
			}
			if(session.getAttribute(titem.get("titemtypes").toString())==null) {
				list=new ArrayList();
			}else {
				list=(List<Integer>) session.getAttribute(titem.get("titemtypes").toString());
			}
			list.add(id);
			session.setAttribute(titem.get("titemtypes").toString(), list);
			return obj;
		}	
		@RequestMapping("shanchu")
		@ResponseBody
		public void shanchu(Integer id,String str,HttpSession session) {
			List<Integer> list=(List<Integer>) session.getAttribute(str);
			list.remove(id);
		}
		@RequestMapping("add-testinserts")
		@ResponseBody
		public void add(Integer Staff_id,Ttest test,Integer[] id) {
			testservice.insertcopany(test, Staff_id);
			testservice.addDuo(testservice.selectid(), id);
		}
		//查看试卷
		@RequestMapping("looktest")
		public void looktest(Integer id,Model model) {
			JSONArray array=testservice.selecttitem(id);
			model.addAttribute("ttest",testservice.selecttest(id));
			int i=1;
			for (Object o : array) {
				JSONObject obj=(JSONObject)o;
				obj.put("sta", i);
				if(obj.get("titemtypes").equals("单选")||obj.get("titemtypes").equals("多选")) {
					JSONObject oo=testservice.findByIdDai(obj.getInteger("titemid"));
					obj.put("a", oo.get("tanswerA"));
					obj.put("b", oo.get("tanswerB"));
					obj.put("c", oo.get("tanswerC"));
					obj.put("d", oo.get("tanswerD"));
				}
				i++;
			}
			model.addAttribute("ttiem", array);
		}
		//初始化
		@RequestMapping("personcsh")
		@ResponseBody
		public JSONArray selectallperson(Integer ttestid) {
			JSONArray json = testservice.findByIdtest(ttestid);	
			return json;
		}
		/**查看考生
		 * @return
		 */
		@RequestMapping("look-student")
		@ResponseBody
		public void insertstudent(Integer ttestid) {
			testservice.insertstudent(ttestid);
		}
		//通过试卷id查看考生
		@RequestMapping("ttest-selectsatff")
		@ResponseBody
		public JSONArray selectsatff(Integer ttestid) {
			return testservice.selectTStaff(ttestid);
		}
		//查找试卷
				@RequestMapping("select_shijuans")
				@ResponseBody
				public JSONArray select_interviewer() {
					JSONArray json = testservice.selecttest();
					int i=1;
					for (Object object : json) {
						JSONObject obj=(JSONObject)object;
						obj.put("sortable", i);
						i++;
					}
					return json;
				}
}